package me.softwareengineer.guice.rendering.domain;

import java.util.List;
import java.util.Map;

import me.softwareengineer.guice.rendering.domain.referenece.ResourceReference;
import me.softwareengineer.guice.rendering.domain.search.ElementFilter;
import me.softwareengineer.guice.rendering.listener.EndLinkEvent;
import me.softwareengineer.guice.rendering.listener.Listener;
import me.softwareengineer.guice.rendering.listener.StartLinkEvent;

/**
 * Represents link element.
 * 
 * @author yadtahir
 * 
 *         <pre>
 * This class is thread-safe and can be cached/re-used and used concurrently.
 * </pre>
 */
public class LinkElement extends Element {

	/**
	 * A reference to the link target. See
	 * {@link org.xwiki.rendering.listener.reference.ResourceReference} for more
	 * details.
	 */
	private ResourceReference reference;
	/**
	 * If true then the link is defined as a free standing URI directly in the
	 * text.
	 */
	private final boolean isFreeStandingURI;

	/**
	 * Creates an link element
	 * 
	 * @param children
	 *            the children of the element. For more info, see
	 *            {@link HasProperties}
	 * @param reference
	 *            the reference of the image
	 * @param isFreeStandingURI
	 *            true if the image is defined as a free standing URI directly
	 *            in the text.
	 */
	public LinkElement(List<Element> children, ResourceReference reference,
			boolean isFreeStandingURI) {
		super(children);
		this.reference = reference;
		this.isFreeStandingURI = isFreeStandingURI;
	}

	/**
	 * Creates an link element
	 * 
	 * @param children
	 *            the children of the element. For more info, see
	 *            {@link HasProperties}
	 * @param reference
	 *            the reference of the image
	 * @param isFreeStandingURI
	 *            true if the image is defined as a free standing URI directly
	 *            in the text.
	 * @param properties
	 *            the properties of the element
	 */
	public LinkElement(List<Element> children, ResourceReference reference,
			boolean isFreeStandingURI, Map<String, String> properties) {
		super(children, properties);
		this.reference = reference;
		this.isFreeStandingURI = isFreeStandingURI;
	}

	@Override
	public void afterElement(Listener listener) {
		EndLinkEvent e = new EndLinkEvent(getReference(), isFreeStandingURI(),
				getAllProperties());
		listener.onEvent(e);
	}

	@Override
	public void beforeElement(Listener listener) {
		StartLinkEvent e = new StartLinkEvent(getReference(),
				isFreeStandingURI(), getAllProperties());
		listener.onEvent(e);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public LinkElement clone(ElementFilter blockFilter) {
		LinkElement clone = (LinkElement) super.clone(blockFilter);
		clone.reference = reference.clone();
		return clone;
	}

	/**
	 * 
	 * @return the reference of the link element
	 * <p>
	 * This property is immutable and thread-safe
	 * </p>
	 */
	public ResourceReference getReference() {
	
			return reference;
		
	}

	/**
	 * 
	 * @return true if the link is defined as a free standing URI directly in
	 *         the text.
	 * <p>
	 * This property is immutable and thread-safe
	 * </p>
	 */
	public boolean isFreeStandingURI() {
		
			return isFreeStandingURI;
	

	}



}
